﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using TotalApplication.Core;
using TotalApplication.Mine_Video;

namespace TotalApplication.viewmodels.Video.Controls
{
    public partial class TagItemVM : BaseViewModel
    {
        #region Properties

        [ObservableProperty]
        private string _content = "";

        [ObservableProperty]
        private string _editContent = "";

        [ObservableProperty]
        private TagLIstVM _childs = new TagLIstVM();

        /// <summary>
        /// 状态变量
        /// </summary>
        [ObservableProperty]
        private bool _isEdit = false;

        [ObservableProperty]
        private bool _isExpanded = false;

        [ObservableProperty]
        private bool _isTop = false;

        [ObservableProperty]
        private bool _isChild = false;

        [ObservableProperty]
        public bool _hasChild = false;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor
        /// </summary>
        public TagItemVM()
        {
            Childs.Parent = this;
        }

        #endregion

        #region Command

        [RelayCommand]
        public void Click()
        {
            if (IsEdit)
            {
                // 进入编辑状态
                Content = EditContent;
                IsEdit = false;
            }
            else
            {
                // 进入详情界面
                EditContent = Content;
                IsEdit = true;
            }
        }

        [RelayCommand]
        public void RightClick()
        {

        }

        [RelayCommand]
        public void IconClick()
        {
            if (IsEdit)
            {
                VideoModelContainer.TagsVM.Tags.Delete(this);
            }
            else
            {
                VideoModelContainer.TagsVM.Tags.Top(this);
            }
        }

        public void Expand()
        {
            VideoModelContainer.TagsVM.Tags.Expand(this);
        }

        #endregion

        #region Tools

        // 通过 UI 元素查找所属的 TagListVM
        public TagLIstVM? FindParentListFromUI(FrameworkElement currentElement)
        {
            // 1. 向上查找最近的 ItemsControl（如 ListBox）
            ItemsControl? parentItemsControl = FindVisualParent<ItemsControl>(currentElement);
            if (parentItemsControl == null)
                return null;

            // 2. 获取 ItemsControl 的 DataContext（即 TagListVM）
            return parentItemsControl.DataContext as TagLIstVM;
        }

        // 泛型方法：查找视觉树上的父控件
        private static T? FindVisualParent<T>(DependencyObject child) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);
            while (parent != null && !(parent is T))
                parent = VisualTreeHelper.GetParent(parent);
            return parent as T;
        }

        #endregion
    }
}
